package com.hsyt.pms.sys.dubbo.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hsyt.pfc.support.rpc.PageResult;
import com.hsyt.pms.api.dto.system.SysFunctionDto;
import com.hsyt.pms.api.entity.system.SysFunction;
import com.hsyt.pms.api.vo.system.SysFunctionVo;
import com.hsyt.pms.api.vo.system.TreeNode;
import com.hsyt.pms.orm.sys.SysFunctionDao;
import com.hsyt.pms.sys.dubbo.SysFunctionService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: SysFunctionServiceImpl
 * @Description: 系统菜单业务层实现
 * @author wangze@hsyuntai.com
 * @date 2019年2月12日 下午2:45:51
 */
@Slf4j
@Component
@Service(version = "0.0.1", interfaceClass = SysFunctionService.class, registry = "pms", group = "pmsSystem", timeout = 3000)
public class SysFunctionServiceImpl implements SysFunctionService {

    private static final Logger LOGGER = LoggerFactory.getLogger(SysFunctionServiceImpl.class);

    @Resource
    private SysFunctionDao pmsFunctionDao;

    @Override
    public Boolean addFunction(SysFunctionDto pmsFunctionDto) {
        return pmsFunctionDao.insertSelective(pmsFunctionDto) == 1;
    }

    @Override
    public Boolean updateFunctionByKeySelective(SysFunctionDto pmsFunctionDto) {
        return pmsFunctionDao.updateByPrimaryKeySelective(pmsFunctionDto) == 1;
    }

    @Override
    public List<TreeNode> getMenus(Long userId, SysFunctionDto dto) {
        return pmsFunctionDao.getMenus(userId, dto);
    }

    @Override
    public int getFunctionCount(SysFunctionDto pmsFunctionDto) {
        return pmsFunctionDao.getFunctionCount(pmsFunctionDto);
    }

    @Override
    public List<SysFunctionVo> getLeftMenuList(Long id) {
        List<SysFunctionVo> listVo = new ArrayList<SysFunctionVo>();
        if (id.longValue() == 1) {
            List<SysFunction> pfs =  pmsFunctionDao.adminGetLeftMenuList();
            if(CollectionUtils.isEmpty(pfs)){
                return listVo;
            }
            for(SysFunction pf :pfs){
                SysFunctionVo vo = new SysFunctionVo();
                BeanUtils.copyProperties(pf, vo);
                List<SysFunction> subPf = pmsFunctionDao.adminGetSubLeftMenuList(pf.getCode());
                if(CollectionUtils.isNotEmpty(subPf)){
                    vo.setChildMenus(subPf);
                }
                listVo.add(vo);
            }
        } else {
            List<SysFunction> pfs =  pmsFunctionDao.getLeftMenuList(id);
            if(CollectionUtils.isEmpty(pfs)){
                return listVo;
            }
            for(SysFunction pf :pfs){
                SysFunctionVo vo = new SysFunctionVo();
                BeanUtils.copyProperties(pf, vo);
                List<SysFunction> subPf = pmsFunctionDao.getLeftSubMenuList(id, pf.getCode());
                if(CollectionUtils.isNotEmpty(subPf)){
                    vo.setChildMenus(subPf);
                }
                listVo.add(vo);
            }
        }
        return listVo;
    }

    @Override
    public PageResult<SysFunctionVo> searchPageDataByDto(SysFunctionDto dto) {
        Page<?> page = PageHelper.startPage(dto.getPageNumber(),dto.getPageSize(), true);
        List<SysFunctionVo> list = pmsFunctionDao.queryFunctionByDto(dto);
        PageResult<SysFunctionVo> pageResult =  PageResult.create(page.getTotal(), list);
        return pageResult;
    }
}
